// pages/settlement/AddOneGoods/AddOneGoods.js
const controllers = require('../../controllers/controllerIndex.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    labels:[],
    rightList:[],
    
    labelId:0,
    navchoice:0,
    imgurl:'test',
    pageNumber: 1,//页数
    pageSize:5,//页大小
    chooseList:[],
    chooseProduct:[],
    onlyone:false,
  }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this
    console.log(options)
    if( options.nowList && options.nowList!='undefined'&& options.nowList.length>0){
      let  chooseList =JSON.parse( options.nowList)
      that.setData({
        chooseList: chooseList,
        chooseProduct:chooseList
      });
    }
    if(options.onlyone){
      this.setData({
        onlyone:options.onlyone
      })
    }
    if(options.type){
      this.setData({
        type:options.type
      })
    }
  
    controllers.selectLabel({
      type: 1,//0默认1商城页面分类列表2店铺入驻分类列表3系统新增商城分类
      shopId:wx.getStorageSync('shopId'),
      
    }).then(res =>{
      if(res.data.length>0){
     that.setData({
      labels:res.data,
      navchoice:res.data[0].id
      })
      that.getList(1);}
    })
    
  },
  lower(e){
   console.log(e)
    let that =this
    if(!that.data.lastPage){
      let p=that.data.pageNumber
     
      that.getList(p+1)
    }
  },
  //右侧内容
  getList(page){
    var that =this;
    var page=page||1
   
    var jsonInfo ={
      shopId:wx.getStorageSync('shopId'),
      labelId: that.data.navchoice,
      pageSize:20,
      pageNumber:page,
    }



    controllers.labelGoodsPage(jsonInfo).then(res =>{
     
      let   rightList=that.data.rightList
       
        if(res.data.pageNumber!=1){
         
          console.log(1,res.data.list)
          rightList=rightList.concat(res.data.list)
          console.log(2,rightList)
        }
        else{
          rightList=res.data.list
          console.log(3,rightList)
        
        }

      // for (var index in rightList){
      //   rightList[index].count =1;
      // }
      
      that.setData({
        rightList:rightList,
        lastPage: res.data.lastPage,
        pageNumber:res.data.pageNumber
      })

      
      rightList.forEach(function (v, index) {
        that.setData({
          [`rightList[${index}].count`]:1
        })

        
        that.data.chooseList.forEach(item => {
          if (item.id == v.id) {
            that.setData({
              [`rightList[${index}].checked`]: true,
              [`rightList[${index}].count`]:item.count,
             
            });
          }
        })
 
      
     })


    })
  },
  
  // add(res){
  //   console.log(res)
  //     let that =this
  //   that.data.rightList.forEach(function (v, index) {
  //     if (res.target.dataset.id == v.id) {
  //       if(v.type==0){
  //         that.setData({
  //           [`rightList[${index}].count`]:1,
  //           [`rightList[${index}].checked`]:true
  //         })
  //       }else if(v.type == 1){
  //         that.setData({
  //           [`rightList[${index}].count`]:res.detail,
  //           [`rightList[${index}].checked`]:true
  //         })
  //       }else{
  //         wx.showToast({
  //           title: '无法添加云商品',
  //           icon: 'none',
  //           duration: 2000
  //         })
           
  //       }
    
  //   }
  //   })
  // },

  //点击左边导航
  navchoice(e){
    this.setData({
      navchoice:e.currentTarget.id,
      pageNumber: 1,  scrollTop:0
    })
    this.getList(1);
  },

  //选中
  checkboxChange(e) {
    console.log(e);
    let list=this.data.rightList
    let id=e.detail.value
    let chooseProduct=[]
    list.forEach(item=>{
      if(item.id==id){
        chooseProduct.push(item)
      }
    })
  
    this.setData({
      chooseProduct:chooseProduct,
      chooseList:chooseProduct
    })
  },
  findIndex(value){
   return this.data.chooseProduct.findIndex(item => {
      return item.id == value;
  });

  },

  cancel(){
    wx.navigateBack({
  
    })

  },
  returnBychoose(){
   
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  //上一个页面
    let chooseProduct =this.data.chooseProduct;
    prevPage.setData({
      chooseProduct: chooseProduct
    })
    prevPage.computedCost()
    wx.navigateBack({
  
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})